package com.jindy.module.outboundorder.service.impl;


import com.jindy.module.constant.StatusConstants;
import com.jindy.module.machine.domain.MachineRecord;
import com.jindy.module.machine.mapper.MachineRecordMapper;
import com.jindy.module.order.domain.Order;
import com.jindy.module.order.domain.OrderProduct;
import com.jindy.module.order.mapper.OrderMapper;
import com.jindy.module.order.mapper.OrderProductMapper;
import com.jindy.module.outboundorder.domain.OutboundOrderMachine;
import com.jindy.module.outboundorder.mapper.OutboundOrderMachineMapper;
import com.jindy.module.outboundorder.service.DepositWarningService;
import com.jindy.module.outboundorder.domain.OutboundOrder;
import com.jindy.module.outboundorder.mapper.OutboundOrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 押金预警Service接口实现类
 *
 * @author zhaoxi
 * @date 2022-11-15
 */

@Service
public class DepositWarningServiceImpl implements DepositWarningService {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OutboundOrderMapper outboundOrderMapper;
    @Resource
    private OutboundOrderMachineMapper outboundOrderMachineMapper;
    @Resource
    private OrderProductMapper orderProductMapper;
    @Resource
    private MachineRecordMapper machineRecordMapper;

    @Override
    public List<Order> selectWarningOrderList(Order order) {
        Order order1 = new Order();
        order1.setOrderStatus(StatusConstants.OrderStatusConstant.orderStatus_2);//所有出库未回订单
        List<Order> list = orderMapper.selectOrderList(order1);
        List<Order> list2 = new ArrayList<>();
        List<MachineRecord> list3 = new ArrayList<>();

        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(or -> {
                //订单中的应收押金
                BigDecimal deposit = or.getDepositReceivable();
                //订单中所有机器记录
                MachineRecord machineRecord = new MachineRecord();
                machineRecord.setOrderId(or.getId());
                List<MachineRecord> machineRecordList = machineRecordMapper.selectMachineRecordList(machineRecord);
                machineRecordList.forEach(machineRecord1 -> {
                    //筛选机器使用记录表中，所有出库机器的记录
                    if (machineRecord1.getOutboundTime() != null && machineRecord1.getWarehousingTime() == null) {
                        //获取此台机器出库天数
                        Date date1 = machineRecord1.getOutboundTime();
                        Date date2 = new Date();
                        differentDays(date1, date2);
                        String strDifferentDays = String.valueOf(differentDays(date1, date2));
                        //根据条件获取出库产品列表
                        OutboundOrderMachine outMachine = new OutboundOrderMachine();
                        outMachine.setOutboundOrderId(machineRecord1.getOutboundId());
                        outMachine.setMachineId(machineRecord1.getMachineId());
                        List<OutboundOrderMachine> outMachineList = outboundOrderMachineMapper.selectOutboundOrderMachineList(outMachine);
                        if (!CollectionUtils.isEmpty(outMachineList)) {//只有一条
                            //出库机器列表内该机器的产品id
                            Long productId = outMachineList.get(0).getProductId();
                            //获得该产品日租金
                            OrderProduct orderProduct = new OrderProduct();
                            orderProduct.setOrderId(machineRecord1.getOrderId());
                            orderProduct.setProductId(productId);
                            List<OrderProduct> orderProductList = orderProductMapper.selectOrderProductList(orderProduct);
                            if (!CollectionUtils.isEmpty(orderProductList)) {
                                BigDecimal dailyRent = orderProductList.get(0).getDailyRent();//相同productId;产品日租金相同
                                //单台总租金放入机器记录表
                                BigDecimal num1 = new BigDecimal(strDifferentDays);
                                BigDecimal rentPaid = dailyRent.multiply(num1);
                                machineRecord1.setRentPaid(rentPaid);
                                machineRecordMapper.updateMachineRecord(machineRecord1);
                            }
                        }
                    }
                });
                //机器使用记录表中，该订单所有出库机器数据的条数
                MachineRecord machineRecord1 = new MachineRecord();
                machineRecord1.setOrderId(or.getId());
                List<MachineRecord> machineRecordList1 = machineRecordMapper.selectMachineRecordList(machineRecord1);
                machineRecordList1.forEach(machineRecord2 ->{
                    if (machineRecord2.getOutboundTime() != null && machineRecord2.getWarehousingTime() == null) {
                        list3.add(machineRecord2);
                    }
                });
                //订单出库机器总租金
                BigDecimal rent = list3.stream().map(MachineRecord::getRentPaid).reduce(BigDecimal.ZERO, BigDecimal::add);
                //预警
                if (rent.longValue() >= deposit.longValue() - 500L) {
                list2.add(or);
                }
            });
        }
        return list2;
    }

    /**
     * date2比date1多的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    private int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)   //同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                {
                    timeDistance += 366;
                } else    //不是闰年
                {
                    timeDistance += 365;
                }
            }
            return timeDistance + (day2 - day1);
        } else    //不同年
        {
            return day2 - day1;
        }
    }
}
